<template>
  <div>
    <el-card style="margin-bottom: 20px">
      <el-breadcrumb separator-class="el-icon-arrow-right" style="margin: 20px">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item to="">留言板管理</el-breadcrumb-item>
        <el-breadcrumb-item to="">留言板管理列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row :gutter="20">  
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="search"
            v-on:keyup.enter.native="test"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加留言板</el-button
          >
        </el-col>
      </el-row>
    </el-card>
    <el-card style="margin-bottom: 20px">
      <el-table :data="tableData" height="450px" stripe>
        <el-table-column label="用户ID" prop="userId"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="留言内容" prop="content"></el-table-column>
        <el-table-column label="回复内容" prop="reply"></el-table-column>
        <el-table-column label="回复图片" prop="replyPicture">
          <template slot-scope="scope">
            <img width="100px" :src="scope.row.replyPicture" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="留言图片" prop="picture">
          <template slot-scope="scope">
            <img width="100px" :src="scope.row.picture" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="updatebefore1(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.$index, scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          background
          :page-size="pageSize"
          @current-change="current"
        >
        </el-pagination>
        <!-- 扶贫政策添加 -->
        <el-dialog
          title="添加留言板信息"
          :visible.sync="addDialogVisible"
          width="40%"
        >
          <el-form :model="addForm">
            <el-form-item label="留言人id:">
              <el-input type="text" v-model="addForm.userId"></el-input>
            </el-form-item>
            <el-form-item label="用户名:">
              <el-input type="text" v-model="addForm.username"></el-input>
            </el-form-item>
            <el-form-item label="留言内容:">
              <el-input type="text" v-model="addForm.content"></el-input>
            </el-form-item>
            <el-form-item label="回复内容:">
              <el-input type="text" v-model="addForm.reply"></el-input>
            </el-form-item>
             <el-form-item label="回复图片:">
              <el-upload
                class="upload-demo"
                action="action"
                accept="image/jpeg,image/png,image/jpg"
                :limit="1"
                :auto-upload="true"
                :http-request="uploadFile"
                ref="upload"
                multiple
              > 
               <!-- :http-request="upload" :http-request="uploadFile" -->
                 <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
            <el-form-item label="留言图片:">
              <el-upload
                class="upload-demo"
                action="action"
                accept="image/jpeg,image/png,image/jpg"
                :limit="1"
                :auto-upload="true"
                :http-request="uploadFile"
                ref="upload"
                multiple
              >
                <!-- :http-request="upload" :http-request="uploadFile"-->
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input type="text" v-model="addForm.createTime"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer">
            <el-button @click="addDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="addMsgboard">确定</el-button>
          </span>
        </el-dialog>
        <!-- 用户编辑 -->
        <el-dialog
          title="编辑用户信息"
          :visible.sync="updateDialogVisible"
          width="40%"
        >
          <el-form :model="updateForm">
            <el-form-item label="留言人id:">
              <el-input type="text" v-model="updateForm.userId"></el-input>
            </el-form-item>
            <el-form-item label="用户名:">
              <el-input type="text" v-model="updateForm.username"></el-input>
            </el-form-item>
            <el-form-item label="留言内容:">
              <el-input type="text" v-model="updateForm.content"></el-input>
            </el-form-item>
            <el-form-item label="留言图片:">
              <el-input type="text" v-model="updateForm.picture"></el-input>
            </el-form-item>
            <el-form-item label="回复内容:">
              <el-input type="text" v-model="updateForm.reply"></el-input>
            </el-form-item>
            <el-form-item label="回复图片:">
              <el-input type="text" v-model="updateForm.replyPicture"></el-input>
            </el-form-item>
            <el-input type="hidden" v-model="updateForm.picture"></el-input>
            <el-input
              type="hidden"
              v-model="updateForm.reply_picture"
            ></el-input>
            <el-form-item label="创建时间">
              <el-input type="text" v-model="updateForm.createTime"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer">
            <el-button @click="updateDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updateMsgboard">确定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      search: "",
      total: 3,
      tableData: [],
      pageSize: 3,
      pageNum: 1,
      addForm: {},
      updateForm: {},
      addDialogVisible: false,
      updateDialogVisible: false,
      nowImg: "",
    };
  },
  methods: {
    async getList(pageNum, pageSize) {
      // let {data:res} = await this.$axios.get('data/user.json');
      // // window.console.log(res.data.list);
      // this.tableData=res.data.list
      let { data: res } = await this.$axios.get("/msgboard/getlist", {
        params: {
          pageNum,
          pageSize,
        },
      });
      // window.console.log(this.pageNum)
      this.total = res.data.total;
      this.tableData = res.data.list;
    },
    current(e) {
      this.pageNum = e;
      this.getList(e, this.pageSize);
    },
    async addMsgboard() {
      this.addForm.picture = this.nowImg;
      window.console.log(JSON.stringify(this.addForm));
      let { data: res } = await this.$axios.post(
        "/msgboard/add",
        JSON.stringify(this.addForm),
        {
          headers: {
            "content-type": "application/json",
          },
        }
      );
      window.console.log(res); //目标值为200
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.addDialogVisible = false;
    },
    async uploadFile() {
      const formData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = {
        headers: { "Content-Type": "multipart/form-data" },
      };
      if (!file) {
        // 若未选择文件
        alert("请选择文件");
        return;
      }
      window.console.log("图片解析为：  " + file.raw);
      formData.append("file", file.raw);
      formData.append("type", "YONGHU");
      let { data: res } = await this.$axios.post(
        "/uploadImg",
        formData,
        headerConfig
      );
      window.console.log(res);
      this.nowImg = res.data.imgurl;
    },
    updatebefore(index, row) {
      // window.console.log(index);
      //  window.console.log(row);
      this.updateForm = row;
      this.updateDialogVisible = true;
    },
    async updatebefore1(index, row) {
      // window.console.log(index);
      //  window.console.log(row);
      let { data: res } = await this.$axios.get("/msgboard/getlist", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: row.id,
        },
      });
      this.updateForm = res.data.list[0];
      this.updateDialogVisible = true;
    },
    async updateMsgboard() {
      //   window.console.log(JSON.stringify(this.updateForm));
      let { data: res } = await this.$axios.post(
        "/msgboard/update",
        this.updateForm
      );
      if (res.code != 200) return this.$message.error(res.code);
      this.$message.success(res.code);
      this.updateDialogVisible = false;
      this.getList(this.pageNum, this.pageSize);
    },
    async del(index, id) {
      //   window.console.log(id);
      let { data: res } = await this.$axios.get("/msgboard/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.code);
      this.getList(this.pageNum, this.pageSize);
    },
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
};
</script>

<style scoped lang="scss">
.el-form {
  border: 1px solid darkblue;
  border-radius: 10px;
  padding: 10px;
  .el-form-item {
    //  border: 1px solid darkblue;
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>